package com.mcptt.http.auth.process.digest.result;

import com.mcptt.http.auth.process.result.AuthResult;

import java.util.HashMap;
import java.util.Map;


public class ThirdGetResult extends AuthResult
{
  private String code = "";
  private String state = "";
  private String error = "";
//  private String errorDescription = "";
//  private String errorUri = "";
  
  private Map<String, String> valueMap = null;
  
  
  public ThirdGetResult(int result,String desc)
  {
    super(result,desc);
  }

  public boolean isSuc()
  {
      if(result == 302
	      && !code.equals(""))
      {
	  return true;
      }
      return false;
  }
  public boolean checkReturnParamOk()
  {
	if(code == null
		|| code.length() <= 0)
	{
	    return false;
	}
	if(state == null
		|| state.length() <= 0)
	{
	    return false;
	}
	return true;
  }
  public boolean checkStateMatch(String oldState)
  {
	if(state != null
		&& oldState != null
		&& state.equals(oldState))
	{
	    return true;
	}
	return false;
  }
  
  public String getErrStr()
  {
	StringBuffer strBuf = new StringBuffer("ThirdGetResult  ");
	strBuf.append("result=").append(result);
	if(desc != null
		&& desc.length() >0)
	{
	    strBuf.append(",");
	    strBuf.append("desc=").append(desc);
	}
	else
	{
	    strBuf.append(";");
	}

	return strBuf.toString();
  }
  
  public void setResultStr(String resultStr)
  {
      parse(resultStr);
      code = getValue("code");
      state = getValue("state");
      error = getValue("error");
  }
  
  private void parse(String resultStr)
  {
	if(resultStr == null
		|| resultStr.length() <= 0)
	{
	    return;
	}
	resultStr = resultStr.trim();

	int index = resultStr.indexOf('?');
	if(index != -1)
	{
	    resultStr = resultStr.substring(index + 1, resultStr.length());
	}

	String[] strArr = resultStr.split("&");
	valueMap = new HashMap<String, String>();
	for (int i = 0; i < strArr.length; i++)
	{
	    String value = strArr[i];
	    String[] valueArr = value.split("=");
	    if (valueArr.length == 2)
	    {
		if (valueArr[0] != null && valueArr[1] != null)
		{
		    String valueStr = valueArr[1].trim();
		    valueMap.put(valueArr[0].trim(), valueStr);
		}
	    }
	}
  }
  
  private String getValue(String key)
  {
	if (key == null || key.equals(""))
	{
	    return "";
	}
	if (valueMap == null || valueMap.size() <= 0)
	{
	    return "";
	}

	String value = valueMap.get(key);
	if (value != null)
	{
	    return value;
	}

	return "";
  }
  
  public String getCode()
  {
    return code;
  }


  public String getState()
  {
    return state;
  }

  public String getError()
  {
    return error;
  }

  public String toString()
  {
	StringBuffer strBuf = new StringBuffer("ThirdGetResult");
	strBuf.append("code=").append(code).append(",");
	strBuf.append("state=").append(state).append(",");
	strBuf.append("error=").append(error).append(",");
	strBuf.append("valueMap=").append(valueMap).append(";");

	return strBuf.toString();
  }

//  public String getErrorDescription()
//  {
//    return errorDescription;
//  }
//
//
//  public void setErrorDescription(String errorDescription)
//  {
//    this.errorDescription = errorDescription;
//  }
//
//
//  public String getErrorUri()
//  {
//    return errorUri;
//  }
//
//
//  public void setErrorUri(String errorUri)
//  {
//    this.errorUri = errorUri;
//  }
}
